var $ = layui.jquery;
var element = layui.element;

layui.use(['baseUtil'], function () {
    var baseUtil = layui.baseUtil;

    //头部左侧菜单点击事件
    element.on('nav(layadmin-layout-left)', function (e) {
        var event = e[0].attributes['layadmin-event'].value;
        if (event === 'flexible') {
            var flexible = $('#LAY_app_flexible');
            if (flexible.hasClass('layui-icon-shrink-right')) {
                $('#LAY_app').addClass('layadmin-side-shrink');
                flexible.removeClass('layui-icon-shrink-right').addClass('layui-icon-spread-left');
            } else {
                $('#LAY_app').removeClass('layadmin-side-shrink');
                flexible.addClass('layui-icon-shrink-right').removeClass('layui-icon-spread-left');
            }
        } else if (event === 'refresh') {
            $('.layui-tab-title li').each(function (i, li) {
                if ($(li).hasClass('layui-this')) {
                    parent.tabRefresh(li.attributes['lay-id'].value);
                }
            });
        }
    });

    //头部右侧菜单点击事件
    element.on('nav(layadmin-layout-right)', function (e) {
        var hasEvent = e[0].attributes.hasOwnProperty('layadmin-event');
        if (hasEvent) {
            var event = e[0].attributes['layadmin-event'].value;
            if (event === 'message') {
                baseUtil.msg('我是谁？我在哪里？我在干嘛？');
            } else if (event === 'logout') {
                baseUtil.confirm('是否确认退出？', '3', function () {
                    baseUtil.doPostData('/system/confirm/logout.do', {}, function () {
                        removeAccessToken();
                        window.location.href = '/login.html';
                    });
                });
            }
        }
        var hasHref = e[0].attributes.hasOwnProperty('lay-href');
        if (hasHref) {
            var id = e[0].attributes['lay-href'].value;
            var title = e[0].innerHTML;
            var url = e[0].attributes['lay-href'].value;
            parent.tabAdd(id, title, url);
        }
        $(e[0].parentNode.parentNode).removeClass('layui-show');
    });

    //左侧菜单点击事件
    element.on('nav(layadmin-system-side-menu)', function (e) {
        var hasHref = e[0].attributes.hasOwnProperty('lay-href');
        if (hasHref) {
            var id = e[0].attributes['lay-href'].value;
            var title = e[0].innerHTML;
            var url = e[0].attributes['lay-href'].value;
            parent.tabAdd(id, title, url);
        }
    });

    function showMenu (layId) {
        var dd = $('.layui-side .layui-nav-child').find('dd');
        dd.removeClass('layui-this');
        dd.each(function (i, e) {
            var hasHref = $(e).find('a')[0].attributes.hasOwnProperty('lay-href');
            if (hasHref) {
                var id = $(e).find('a')[0].attributes['lay-href'].value;
                if (layId === id) {
                    $(e).addClass('layui-this');
                }
            }
        });
    }

    //头部选项卡切换
    element.on('tab(layadmin-layout-tabs)', function (e) {
        if (e.elem.context.attributes) {
            var layId = e.elem.context.attributes['lay-id'].value;
            showMenu(layId);
            parent.tabReSize('change');
        }
    });

    //头部选项卡关闭
    element.on('tabDelete(layadmin-layout-tabs)', function () {
        parent.tabReSize('delete');
    });

    //头部选项卡左定位点击事件
    $('.layui-icon-prev').bind('click', function () {
        parent.tabReSize('left');
    });

    //头部选项卡右定位点击事件
    $('.layui-icon-next').bind('click', function () {
        parent.tabReSize('right');
    });

    //头部选项卡关闭点击事件
    element.on('nav(layadmin-pagetabs-nav)', function (e) {
        var layId;
        var thisIndex = 0;
        $('.layui-tab-title li').each(function (i, e) {
            if ($(e).hasClass('layui-this')) {
                thisIndex = i;
                layId = $(e)[0].attributes['lay-id'].value;
            }
        });
        var layIds = [];
        var event = e[0].parentNode.attributes[0].value;
        if (event === 'closeThisTabs') {
            $('.layui-tab-title li').each(function (i, e) {
                if (i !== 0 && $(e).hasClass('layui-this')) {
                    layIds.push($(e)[0].attributes['lay-id'].value);
                }
            });
        } else if (event === 'closeOtherTabs') {
            $('.layui-tab-title li').each(function (i, e) {
                if (i !== 0 && !$(e).hasClass('layui-this')) {
                    layIds.push($(e)[0].attributes['lay-id'].value);
                }
            });
        } else if (event === 'closeAllTabs') {
            $('.layui-tab-title li').each(function (i, e) {
                if (i !== 0) {
                    layIds.push($(e)[0].attributes['lay-id'].value);
                }
            });
        }
        for (var i = 0; i < layIds.length; i++) {
            $('.layui-tab-title li[lay-id=\'' + layIds[i] + '\']').remove();
            var iframe = $('.layui-tab .layui-tab-content .layui-tab-item iframe');
            for (var j = 1; j < iframe.length; j++) {
                var src = iframe[j].attributes['src'].value;
                if (src.indexOf(layIds[i]) > -1) {
                    $(iframe[j].parentNode).remove();
                }
            }
        }
        if (thisIndex !== 0 && (event === 'closeThisTabs' || event === 'closeAllTabs')) {
            var li = $('.layui-tab-title li');
            var length = li.length;
            layId = li[length - 1].attributes['lay-id'].value;
            $(li[length - 1]).addClass('layui-this');
            $($('.layui-tab-content .layui-tab-item')[length - 1]).addClass('layui-show');
            showMenu(layId);
        }
        $(e[0].parentNode).removeClass('layui-this');
        $(e[0].parentNode.parentNode).removeClass('layui-show');
        parent.tabReSize('change');
    });
});

/**
 * 打开标签(二次打开刷新)
 */
function tabAdd (id, title, url) {
    var isActive = $('.layui-tab .layui-tab-title').find('li[lay-id=\'' + id + '\']');
    if (isActive.length === 0) {
        element.tabAdd('layadmin-layout-tabs', {
            id: id,
            title: title,
            content: '<iframe src="' + url + '" frameborder="0" class="layadmin-iframe"></iframe>'
        });
        element.tabChange('layadmin-layout-tabs', id);
        tabReSize('add');
    } else {
        element.tabChange('layadmin-layout-tabs', id);
        $('.layui-tab .layui-tab-content .layui-show').find('iframe')[0].contentWindow.location.reload();
        tabReSize('change');
    }
}

/**
 * 打开标签(二次打开删除)
 */
function tabChange (id, title, url) {
    var isActive = $('.layui-tab .layui-tab-title').find('li[lay-id=\'' + id + '\']');
    if (isActive.length > 0) {
        element.tabDelete('layadmin-layout-tabs', id);
    }
    element.tabAdd('layadmin-layout-tabs', {
        id: id,
        title: title,
        content: '<iframe src="' + url + '" frameborder="0" class="layadmin-iframe"></iframe>'
    });
    element.tabChange('layadmin-layout-tabs', id);
    tabReSize('add');
}

/**
 * 关闭标签
 */
function tabDelete (id) {
    var isActive = $('.layui-tab .layui-tab-title').find('li[lay-id=\'' + id + '\']');
    if (isActive.length > 0) {
        element.tabDelete('layadmin-layout-tabs', id);
    }
}

/**
 * 刷新标签
 */
function tabRefresh (id) {
    var layId = $('.layui-tab .layui-tab-title li.layui-this').attr('lay-id');
    if (layId === id) {
        $('.layui-tab .layui-tab-content .layui-show').find('iframe')[0].contentWindow.location.reload();
    } else {
        element.tabChange('layadmin-layout-tabs', id);
        $('.layui-tab .layui-tab-content .layui-show').find('iframe')[0].contentWindow.location.reload();
    }
}

/**
 * tab位置重定位
 */
function tabReSize (type) {
    var allWidth = 0;
    if (allWidth === 0) {
        allWidth = $('.layui-tab-title')[0].offsetWidth;
    }
    var tabWidth = 0;
    if (tabWidth === 0) {
        $('.layui-tab-title li').each(function (i, e) {
            tabWidth += e.offsetWidth;
        });
    }
    if (type === 'left') {
        if (tabWidth > allWidth) {
            $('.layui-tab-title').attr('style', 'left:0px');
        }
    } else if (type === 'right' || type === 'add') {
        if (tabWidth > allWidth) {
            $('.layui-tab-title').attr('style', 'left:' + (allWidth - tabWidth) + 'px');
        }
    } else if (type === 'change') {
        tabWidth = 0;
        var li = $('.layui-tab-title li');
        for (var i = 0; i < li.length; i++) {
            tabWidth += li[i].offsetWidth;
            if ($(li[i]).hasClass('layui-this')) {
                break;
            }
        }
        if (tabWidth > allWidth) {
            $('.layui-tab-title').attr('style', 'left:' + (allWidth - tabWidth) + 'px');
        } else {
            $('.layui-tab-title').attr('style', 'left:0px');
        }
    } else if (type === 'delete') {
        if (tabWidth > allWidth) {
            $('.layui-tab-title').attr('style', 'left:' + (allWidth - tabWidth) + 'px');
        } else {
            $('.layui-tab-title').attr('style', 'left:0px');
        }
    }
}
